<meta charset="utf-8">
<?php
//该action文件同时处理来自view模式和code模式的post请求
include 'connect.php';
$mode = $_GET["mode"];
//通过get来传输不同模式，post传输个人信息
if ($mode=="view"){
    $opt = $_GET["opt"];
    $need_num = $_POST['need_num'];
    session_start();
    $user_id = $_SESSION['user'];
    $_SESSION['need_num'] = $need_num;
//    如果是报名/打卡就需要知道id和name，否则只需要知道id
    if ($opt!="IN"){
        $id = $_POST['id'];
    }else{
        $id = $_POST['id'];
        $name = $_POST['name'];
    }
//    定义一个来源网页，方便运行结束后跳转回去
    $html = "index.php";
}elseif ($mode=="code"){
    $need_num = $_POST['need_num'];
    session_start();
    $user_id = $_SESSION['user'];
    $_SESSION['need_num'] = $need_num;
    $code = $_POST['code'];
//    使用explode函数对输入的code进行一个分割
    $action_list = explode(" ",$code);
    $opt = $action_list[0];
    if ($opt!="IN"){
        $id = $action_list[1];
    }else{
        $id = $action_list[1];
        $name = $action_list[2];
    }
//    定义一个来源网页，方便运行结束后跳转回去
    $html = "index_code.php";
}
//这个if处理报名/打卡（代码为IN）的请求
if ($opt=="IN") {
    $query_select = "SELECT * FROM `$user_id` WHERE id = '$id'";//  构建查询语句
    $result_select = mysqli_query($connection, $query_select);
    if (!$result_select) {
    } else {
        $num = mysqli_num_rows($result_select);
        if ($num == 0) {
//            数据库中没有信息（该id记录为0），说明是新报名，插入他的信息
            $time = time();
            $query_insert = "INSERT INTO `$user_id` (`id`, `name`, `active_time`) VALUES ($id, '$name', '$time')";//构建插入语句
            $result_insert = mysqli_query($connection, $query_insert);//执行插入
            if (!$result_insert) {//意味着无法插入这个用户的信息
                echo '发生未知错误，错误码：0001';
                exit();
            } else {
//                判断有没有挤掉别人
                $query_select2 = "SELECT * FROM `$user_id`";//  构建查询语句
                $result_select2 = mysqli_query($connection, $query_select2);
                $num = mysqli_num_rows($result_select2);
                while ($num>=($need_num+1)){
                    #说明超人啦！要移除最晚活跃的同学
                    $query_delete2 = "SELECT * FROM `$user_id` ORDER BY active_time LIMIT 1";//  构建查询语句
                    $result_detele2 = mysqli_query($connection, $query_delete2);//执行插入
                    while ($row = mysqli_fetch_row($result_detele2)) {
                        $id = $row[0];
//                        获取到最晚活跃的同学的ID，然后进行移除
                        $query_delete = "DELETE FROM `$user_id` WHERE `$user_id`.`id` = $id";//  构建查询语句
                        $result = mysqli_query($connection, $query_delete);
                    }
//                    接着再获取选择的人数，再超过就再进行本循环移除同学
                    $result_select2 = mysqli_query($connection, $query_select2);
                    $num = mysqli_num_rows($result_select2);

                }
                $_SESSION['action_log'] = "报名成功";
                echo '<body>
<script language=\'javascript\'>alert("运行结果：报名成功");document.location = '.'"'.$html.'"'.'</script>
</body>';
                exit();
            }
        }else{
//            说明是已经报过名，回来活跃的
            $query_select = "SELECT * FROM `$user_id` WHERE id = '$id'";//  构建查询语句
            $result_select = mysqli_query($connection, $query_select);
            if (!$result_select) {

            }else{
//                能准确找到同学的信息 先进行删除操作
                $query_delete = "DELETE FROM `$user_id` WHERE `$user_id`.`id` = $id";//  构建查询语句
                $result = mysqli_query($connection, $query_delete);
//                再进行插入操作
                $time = time();
                $query_insert = "INSERT INTO `$user_id` (`id`, `name`, `active_time`) VALUES ($id, '$name', '$time')";//构建插入语句
                $result_insert = mysqli_query($connection, $query_insert);//执行插入
                $_SESSION['action_log'] = "打卡成功";
                echo '<body>
<script language=\'javascript\'>alert("运行结果：打卡成功");document.location = '.'"'.$html.'"'.'</script>
</body>';
            }
        }
    }
}

//这个if用来处理管理员查看/LOOK请求
if ($opt=="LOOK"){
    $query_select = "SELECT * FROM `$user_id` WHERE id = '$id'";//  构建查询语句
    $result_select = mysqli_query($connection, $query_select);
    if (!$result_select) {

    } else {
        $num = mysqli_num_rows($result_select);
        if ($num == 0) {
//            数据库中没有信息，报错
            $_SESSION['action_log'] = "ERROR";
            echo '<body>
<script language=\'javascript\'>alert("运行结果：ERROR");document.location = '.'"'.$html.'"'.'</script>
</body>';
        }else{
//            数据库中有信息，获取该条信息的name
            $result_select = mysqli_query($connection, $query_select);
            while ($row = mysqli_fetch_row($result_select)) {
                $name = $row[1];
                $_SESSION['action_log'] = '姓名为：'.$name;
                echo '<body>
<script language=\'javascript\'>alert("运行结果：'.$_SESSION['action_log'].'");document.location = '.'"'.$html.'"'.'</script>
</body>';
            }
        }
    }
}

//这个if用来查看移除/OUT请求
if ($opt=="OUT") {
    $query_select = "SELECT * FROM `$user_id` WHERE id = '$id'";//  构建查询语句
    $result_select = mysqli_query($connection, $query_select);
    if (!$result_select) {

    } else {
        $num = mysqli_num_rows($result_select);
        if ($num == 0) {
//            数据库中没有信息，报错
            $_SESSION['action_log'] = "ERROR";
            echo '<body>
<script language=\'javascript\'>alert("运行结果：ERROR");document.location = '.'"'.$html.'"'.'</script>
</body>';
        } else {
//            找到这位同学，开始在数据库中移除他的信息
            $query_delete = "DELETE FROM `$user_id` WHERE `$user_id`.`id` = $id";//  构建查询语句
            $result_detele = mysqli_query($connection, $query_delete);
            if (!$result_detele) {

            }else{
                $_SESSION['action_log'] = "移除成功";
                echo '<body>
<script language=\'javascript\'>alert("运行结果：移除成功");document.location = '.'"'.$html.'"'.'</script>
</body>';
            }
        }
    }
}
